package com.neusoft.NursingHome.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.NursingHome.po.ResponseBean;
import com.neusoft.NursingHome.po.User;
import com.neusoft.NursingHome.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取所有的健康管家列表
     * @param type 用户类型（健康管家）
     * @return 包含健康管家列表的 ResponseBean
     */
    @GetMapping("/healthmanagers/{type}")
    public ResponseBean<List<User>> getAllHealthManagers(@PathVariable Integer type) {
        List<User> healthManagers = userService.getAllHealthManagers(type);
        return new ResponseBean<>(healthManagers);
    }

    /**
     * 按健康管家姓名模糊查询
     * @param type 用户类型（健康管家）
     * @param name 健康管家姓名（支持模糊匹配）
     * @return 包含匹配的健康管家列表的 ResponseBean
     */
    @GetMapping("/healthmanagers/search")
    public ResponseBean<List<User>> searchHealthManagersByName(
            @RequestParam Integer type,
            @RequestParam String name) {
        List<User> healthManagers = userService.searchHealthManagersByName(type, name);
        return new ResponseBean<>(healthManagers);
    }

    /**
     * 获取所有的用户列表
     * @return 包含所有用户列表的 ResponseBean
     */
    @GetMapping("/all")
    public ResponseBean<List<User>> getAllUsers() {
        List<User> users = userService.getAllUsers();
        return new ResponseBean<>(users);
    }

    /**
     * 按照姓名模糊搜索用户
     * @param name 用户姓名（支持模糊匹配）
     * @return 包含匹配的用户列表的 ResponseBean
     */
    @GetMapping("/searchByName/{name}")
    public ResponseBean<List<User>> searchUsersByName(@PathVariable String name) {
        List<User> users = userService.searchUsersByName(name);
        return new ResponseBean<>(users);
    }

    /**
     * 添加新用户
     * @param user 用户对象
     * @return 添加结果的 ResponseBean
     */
    @PostMapping("/add")
    public ResponseBean<Boolean> addUser(@RequestBody User user) {
        boolean result = userService.addUser(user);
        return new ResponseBean<>(result);
    }

    /**
     * 更新用户信息
     * @param user 更新后的用户对象
     * @return 更新结果的 ResponseBean
     */
    @PutMapping("/update")
    public ResponseBean<Boolean> updateUser(@RequestBody User user) {
        boolean result = userService.updateUser(user);
        return new ResponseBean<>(result);
    }

    /**
     * 逻辑删除用户
     * @param userId 用户的唯一标识符
     * @return 删除结果的 ResponseBean
     */
    @DeleteMapping("/delete/{userId}")
    public ResponseBean<Boolean> deleteUser(@PathVariable Integer userId) {
        boolean result = userService.deleteUser(userId);
        return new ResponseBean<>(result);
    }
}